package com.mb.app.service.impl;

import com.mb.app.domain.SysAreaCoord;
import com.mb.app.service.IAddrService;
import com.mb.app.service.ISysAreaCoordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("addrService")
@Slf4j
public class AddrServiceImpl implements IAddrService {

    @Autowired
    private ISysAreaCoordService sysAreaCoordService;

    @Override
    public String getAddrName(String areaId) {
        String addr = "";
        SysAreaCoord sysAreaCoord = new SysAreaCoord();
        sysAreaCoord.setId(areaId);
        sysAreaCoord.setStatus(1);
        SysAreaCoord sysAreaCoord1 = sysAreaCoordService.selectByAreaCoord(sysAreaCoord);
        if (null != sysAreaCoord1) {
            addr = sysAreaCoord1.getName() + addr;
            if (null != sysAreaCoord1.getParentId() && "" != sysAreaCoord1.getParentId() ) {
                sysAreaCoord.setId(sysAreaCoord1.getParentId());
                SysAreaCoord sysAreaCoord2 = sysAreaCoordService.selectByAreaCoord(sysAreaCoord);
                if (null != sysAreaCoord2) {
                    addr = sysAreaCoord2.getName() + addr;
                    if (null != sysAreaCoord2.getParentId() && "" != sysAreaCoord2.getParentId()) {
                        sysAreaCoord.setId(sysAreaCoord2.getParentId());
                        SysAreaCoord sysAreaCoord3 = sysAreaCoordService.selectByAreaCoord(sysAreaCoord);
                        if (null != sysAreaCoord3) {
                            if (sysAreaCoord3.getName().length() > 2) {
                                addr = sysAreaCoord3.getName() + addr;
                            }
                            if (null != sysAreaCoord3.getParentId() && "" != sysAreaCoord3.getParentId()) {
                                sysAreaCoord.setId(sysAreaCoord3.getParentId());
                                SysAreaCoord sysAreaCoord4 = sysAreaCoordService.selectByAreaCoord(sysAreaCoord);
                                if (null != sysAreaCoord4) {
                                    if (sysAreaCoord4.getName().length() > 2) {
                                        addr = sysAreaCoord4.getName() + addr;
                                    }
                                }else {
                                    return addr;
                                }
                            }
                        }else {
                            return addr;
                        }
                    }
                }else {
                    return addr;
                }
            }
        }else {
            return addr;
        }
        return  addr;
    }

    @Override
    public String getProAreaId(String areaId) {
        SysAreaCoord sysAreaCoord = new SysAreaCoord();
        String id = "";
        sysAreaCoord.setId(areaId);
        sysAreaCoord.setStatus(1);
        SysAreaCoord sysAreaCoord1 = sysAreaCoordService.selectByAreaCoord(sysAreaCoord);
        if (sysAreaCoord1 != null) {
            if (sysAreaCoord1.getLevel() != null && sysAreaCoord1.getLevel() == 1) {
                id = sysAreaCoord1.getId();
                return  id;
            }else {
                sysAreaCoord.setId(sysAreaCoord1.getParentId());
                SysAreaCoord sysAreaCoord2 = sysAreaCoordService.selectByAreaCoord(sysAreaCoord);
                if (sysAreaCoord2 != null) {
                    if (sysAreaCoord2.getLevel() != null && sysAreaCoord2.getLevel() == 1) {
                        id = sysAreaCoord2.getId();
                        return  id;
                    }else {
                        sysAreaCoord.setId(sysAreaCoord2.getParentId());
                        SysAreaCoord sysAreaCoord3 = sysAreaCoordService.selectByAreaCoord(sysAreaCoord);
                        if (sysAreaCoord3 != null) {
                            if (sysAreaCoord3.getLevel() != null && sysAreaCoord3.getLevel() == 1) {
                                id = sysAreaCoord3.getId();
                                return id;
                            }else {
                                sysAreaCoord.setId(sysAreaCoord3.getParentId());
                                SysAreaCoord sysAreaCoord4 = sysAreaCoordService.selectByAreaCoord(sysAreaCoord);
                                if (sysAreaCoord4 != null) {
                                    if (sysAreaCoord4.getLevel() != null && sysAreaCoord4.getLevel() == 1) {
                                        id = sysAreaCoord4.getId();
                                        return id;
                                    }else {
                                        sysAreaCoord.setId(sysAreaCoord4.getParentId());
                                        SysAreaCoord sysAreaCoord5 = sysAreaCoordService.selectByAreaCoord(sysAreaCoord);
                                        if (sysAreaCoord5 != null) {
                                            if (sysAreaCoord5.getLevel() != null && sysAreaCoord5.getLevel() == 1) {
                                                id = sysAreaCoord5.getId();
                                                return id;
                                            }else {
                                                id = "";
                                                return id;
                                            }
                                        }else {
                                            id = "";
                                            return id;
                                        }
                                    }
                                }else {
                                    id = "";
                                    return id;
                                }
                            }
                        }else {
                            id = "";
                            return  id;
                        }
                    }
                }else {
                    id = "";
                    return id;
                }
            }
        } else {
            id = "";
            return id;
        }
    }
}
